package softtech.apps.autoanswerphonecall;


import java.util.List;
import softtech.apps.autoanswerphonecall.R;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

public class MessageManagerActivity extends Activity {

	ListView lv;
	private final String TAG = "Tag";
	List<Message> lstMessage;
	messageAdapter mAdapter;
	DatabaseHandler db;
	AlertDialogManager ad;
	
	Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_manager);
		
		ActionBar bar = getActionBar();
		bar.setDisplayHomeAsUpEnabled(true);
		bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2ac2d4"))); 
		context = this;
		
		db = new DatabaseHandler(this);

		
		lv = (ListView) findViewById(R.id.lvMessage);
		lstMessage = db.getAllMessages();
		
		//Log.d(TAG, "List message = "+lstMessage.size());
		mAdapter = new messageAdapter(context, lstMessage);
		
		
		lv.setAdapter(mAdapter);
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				//Toast.makeText(getApplicationContext(), "Item on click",Toast.LENGTH_SHORT).show();
				// TODO Auto-generated method stub
				//Toast.makeText(context, "Item on click",Toast.LENGTH_SHORT).show();
				//Log.d(TAG, "Position = "+position);
				
				// Lopp and Update all message SET is_choose = 0
				 for (Message m : lstMessage){
					 db.updateMessageChoose(m.get_id());
				 }

				// update current choose
				Message mes = new Message(lstMessage.get(position).get_id(),lstMessage.get(position).get_message(),1);
				db.updateMessage(mes);
				
				lstMessage = db.getAllMessages();
				mAdapter = new messageAdapter(context, lstMessage);
				lv.setAdapter(mAdapter);
			}
			
		});
		ImageView imvAdd = (ImageView) findViewById(R.id.imgbtnAddMessage);
		imvAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText etMessage = (EditText)findViewById(R.id.etMessage);
				if(etMessage.getText().toString().length() == 0){
					ad = new AlertDialogManager();
					ad.showAlertDialog(context, "Error", "Message don't leave blank", false);
				}else{
					Message mes = new Message(etMessage.getText().toString(),0);
					db.addMessage(mes);
					lstMessage.add(mes);
					etMessage.setText(null);
					mAdapter = new messageAdapter(context, lstMessage);
					lv.setAdapter(mAdapter);
					 
				}
			}
		});
		
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (item.getItemId()) {
			case android.R.id.home:
		        NavUtils.navigateUpFromSameTask(this);
		        return true;
			default:
		}
		
			return super.onOptionsItemSelected(item);
		
	}
 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu items for use in the action bar
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.message_manager_action_bar, menu);
		return super.onCreateOptionsMenu(menu);
	}

}
